package cn.demomaster.quickzxing;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.google.zxing.Result;
import com.google.zxing.ResultPoint;

public class QuickCaptureActivity extends Activity {

    public static final String CODED_CONTENT = "codedContent";
    //遮盖层视图
    ScanMakerView scanMakerView;
    //背景预览图层
    ScanSurfaceView surfaceView;

    ImageView iv_light;
    Activity mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_capture);
        mContext = this;
        //请求屏幕常亮
        //getActionBarTool().setActionBarType(ACTIONBAR_TYPE.ACTION_STACK);
        //getActionBarTool().setActionBarThemeColors(Color.WHITE,Color.WHITE);
        //getActionBarTool().setHeaderBackgroundColor(Color.TRANSPARENT);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        scanMakerView = findViewById(R.id.viewfinder_view);
        scanMakerView.setMarkerWidth(500);
        scanMakerView.setMarkerHeight(500);
        surfaceView = findViewById(R.id.preview_view);
        surfaceView.setOnScanResultListener(new ScanHelper.OnScanResultListener() {
            @Override
            public void handleDecode(Result obj, Bitmap barcode, float scaleFactor) {
                // Toast.makeText(mContext,obj.toString(),Toast.LENGTH_LONG).show();
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString(CODED_CONTENT, obj.toString());
                intent.putExtras(bundle);
                intent.putExtra(CODED_CONTENT, obj.toString());
                //setResult(PHOTOHELPER_REQUEST_CODE_SCAN_QRCODE, intent);
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void foundPossiblePoint(ResultPoint resultPoint) {
                scanMakerView.foundPossibleResultPoint(resultPoint);
            }
        });

        iv_light = findViewById(R.id.iv_light);
        iv_light.setOnClickListener(v -> {
            if (ScanHelper.getInstance().isFlashOpened(mContext)) {
                ScanHelper.getInstance().closeFlash(mContext);
            } else {
                ScanHelper.getInstance().openFlash(mContext);
            }
        });
        if (ScanHelper.getInstance().isSupportCameraLedFlash(mContext)) {
            iv_light.setVisibility(View.VISIBLE);
        } else {
            iv_light.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onPause() {
        surfaceView.stop();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        surfaceView.start();
    }
}
